package com.lzh.gulimall.product.config;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

/**
 * @ClassName MyThreadConfig
 * @Description 线程池配置
 * @Author liuzhihui
 * @Date 2020/8/30 16:46
 * @Version 1.0
 **/
//开启属性配置，因为ThreadPoolConfigProperties已经通过@Component加入到容器中了,所以不需要了
//@EnableConfigurationProperties 相当于把使用 @ConfigurationProperties 的类进行了一次注入。
//@EnableConfigurationProperties(ThreadPoolConfigProperties.class)
@Configuration
public class MyThreadConfig {
    /**
     * 线程池配置
     * @param threadPoolConfigProperties 容器中已经存在，会自动注入到入参
     * @return
     */
    @Bean
    public ThreadPoolExecutor threadPoolExecutor(ThreadPoolConfigProperties threadPoolConfigProperties){
        return new ThreadPoolExecutor(
                threadPoolConfigProperties.getCoreSize(),
                threadPoolConfigProperties.getMaxSize(),
                threadPoolConfigProperties.getKeepAliveTime(),
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(threadPoolConfigProperties.getCapacity()),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
                );
    }
}
